﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using bdnt_pets_system.SystemServiceReference;
using System.Windows.Media.Imaging;
using System.IO;
using bdnt_pets_system.Tools;
using System.Globalization;
using bdnt_pets_system_Entity;
using System.IO.IsolatedStorage;

namespace bdnt_pets_system.FrameViews
{
    public partial class HeaderControl : UserControl
    {
        public HeaderControl()
        {
            InitializeComponent();
        }
        public int CurrentMenu = 0;
        public void InitMenus(ObservableCollection<Tbl_Menu> allMenus, bool isSystem = false)
        {
            CurrentMenu = isSystem ? 0 : CurrentMenu;
            if (allMenus.Count < 1)
                return;
            spMenus.Children.Clear();
            var cuobjs = allMenus.Where(p => p.MenuType == 1).OrderBy(p => p.MenuSequence).ToList();
            foreach (var item in cuobjs)
            {
                if ((CurrentMenu == 0 && item.MenuName == "系统设置") || CurrentMenu == item.Id)
                {
                    Grid cugd = new Grid()
                    {
                        MinWidth = 120,
                        Height = 32,
                        Margin = new Thickness(0, 0, 20, 0),
                        Background = new ImageBrush { ImageSource = new BitmapImage(new Uri("../Images/headImgs/mBg1.png", UriKind.Relative)) }
                    };
                    HyperlinkButton hb = new HyperlinkButton()
                    {
                        Tag = item,
                        Foreground = new SolidColorBrush(Colors.Black),
                        FontWeight = FontWeights.Bold,
                        HorizontalAlignment = System.Windows.HorizontalAlignment.Center,
                        VerticalAlignment = System.Windows.VerticalAlignment.Center,
                        Cursor = Cursors.Hand,
                        FontSize = 13,
                        Margin = new Thickness(5, 0, 5, 0)
                    };
                    hb.Content = SessionManager.IsCN_ZH ? item.MenuName : item.en_MenuName;
                    hb.Click += new RoutedEventHandler(hlb_Click);
                    cugd.Children.Add(hb);
                    spMenus.Children.Add(cugd);

                }
                else
                {
                    StackPanel sp = new StackPanel()
                    {
                        MinWidth = 90,
                        Height = 28,
                        Margin = new Thickness(0, 0, 20, 0),
                        Orientation = System.Windows.Controls.Orientation.Horizontal,
                        VerticalAlignment = System.Windows.VerticalAlignment.Bottom,
                        Background = new ImageBrush { ImageSource = new BitmapImage(new Uri("../Images/headImgs/mBg.png", UriKind.Relative)) }
                    };

                    Image img = new Image()
                    {
                        Height = 20,
                        Width = 20,
                        Margin = new Thickness(5, 0, 5, 0),
                        HorizontalAlignment = System.Windows.HorizontalAlignment.Left,
                        VerticalAlignment = System.Windows.VerticalAlignment.Center
                    };
                    if (item.Icon != null && item.Icon.Length > 0)
                    {
                        BitmapImage b = new BitmapImage();
                        Stream mm = new MemoryStream();
                        mm.Write(item.Icon, 0, item.Icon.Length);
                        b.SetSource(mm);
                        img.Source = b;
                        sp.Children.Add(img);
                    }
                    HyperlinkButton hb = new HyperlinkButton()
                    {
                        Tag = item,
                        Foreground = new SolidColorBrush(Colors.White),
                        HorizontalAlignment = System.Windows.HorizontalAlignment.Center,
                        VerticalAlignment = System.Windows.VerticalAlignment.Center,
                        Cursor = Cursors.Hand,
                        Margin = new Thickness(0, 0, 15, 0)
                    };

                    hb.Content = SessionManager.IsCN_ZH ? item.MenuName : item.en_MenuName;
                    hb.Click += new RoutedEventHandler(hlb_Click);
                    sp.Children.Add(hb);
                    //Image img1 = new Image()
                    //{
                    //    Height = 10,
                    //    Width = 10,
                    //    Margin = new Thickness(5, 3, 8, 0),
                    //    VerticalAlignment = System.Windows.VerticalAlignment.Center,
                    //    HorizontalAlignment = System.Windows.HorizontalAlignment.Right
                    //};
                    //img1.Source = new BitmapImage(new Uri("../Images/headImgs/mSelected.png", UriKind.Relative));
                    //sp.Children.Add(img1);
                    spMenus.Children.Add(sp);
                }
            }
        }

        void hlb_Click(object sender, RoutedEventArgs e)
        {
            var cuDt = (sender as HyperlinkButton).Tag as Tbl_Menu;
            if (cuDt == null)
                return;
            if (string.IsNullOrEmpty(cuDt.MenuModule))
                return;
            var cuManContent = (this.Tag as Grid);
            if (cuManContent == null)
                return;
            CurrentMenu = cuDt.Id;
            InitMenus(SessionManager.RoleAllMenus);
            App.CurrentParentGrid = cuManContent;
            object obj = Activator.CreateInstance(Type.GetType(cuDt.MenuModule), new object[] { cuDt });
            if (obj != null)
            {
                cuManContent.Children.Clear();
                cuManContent.Children.Add(obj as UIElement);
            }
        }
    }
}
